package com.zl.smartmall.library.listener;

import java.util.List;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.zl.smartmall.library.im.po.MessageInfo;
import com.zl.smartmall.library.util.Constants;
import com.zl.smartmall.library.util.CrashHandler;

/**
 * IM消息发送监听类
 * @author 陈希然
 *
 */
public abstract class MessageListener extends BaseListener {

	private static final String TAG = MessageListener.class.getSimpleName();
	
	public MessageListener() {
		
	}

	@Override
	public void onSuccess(int statusCode, Header[] headers, JSONObject response) {

		try {
			int status = response.getInt(Constants.REQ_STATUS);
			String msg = response.getString(Constants.REQ_MSG);
			CrashHandler.d(TAG, "发送消息服务器status = " + status);
			CrashHandler.d(TAG, "发送消息服务器msg = " + msg);
			
			switch(status){
			case Constants.REQ_OK:
				JSONArray jsonArray = response.getJSONArray(Constants.REQ_RESULT);
				onSendMessageSuccess(MessageInfo.parse(jsonArray, Constants.IM_MSG_SEND_FROM_ME));
				break;
			
			case Constants.REQ_OFFLINE:
				onOffline();
				break;
				
			case Constants.REQ_NO_MORE_DATA:
				onNoMoreData();
				break;
				
			default:
				onResponseFailed();
			}
			
		} catch (JSONException e) {
			onFailure(statusCode, headers, e, new JSONObject());
		}
	}

	@Override
	public void onFailure(int statusCode, Header[] headers,
			Throwable throwable, JSONObject errorResponse) {
		// TODO Auto-generated method stub
		CrashHandler.e(throwable);
		onResponseFailed();
	}
	
	@Override
	public void onNoMoreData() {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void onResponseFailed() {
		// TODO Auto-generated method stub
		onSendMessageFailure();
	}
	
	/**
	 * 发送私信成功
	 * @param messageInfo 
	 */
	public abstract void onSendMessageSuccess(List<MessageInfo> messageInfos);
	
	/**
	 * 发送私信失败
	 */
	public abstract void onSendMessageFailure();
	
	public int describeContents() {
         return 0;
    }

//    public void writeToParcel(Parcel out, int flags) {
//        out.writeInt(requestCode.ordinal());
//    }
//
//    public static final Parcelable.Creator<BaseListener> CREATOR = new Parcelable.Creator<BaseListener>() {
//
//		@Override
//		public BaseListener[] newArray(int size) {
//			return new BaseListener[size];
//		}
//		
//		@Override
//		public BaseListener createFromParcel(Parcel source) {
//			return new SendMessageListener(source){
//
//				@Override
//				public void onSendMessageSuccess(List<MessageInfo> messageInfos) {
//					// TODO Auto-generated method stub
//					
//				}
//
//				@Override
//				public void onSendMessageFailure() {
//					// TODO Auto-generated method stub
//					
//				}
//
//				@Override
//				public void onNoNetWork() {
//					// TODO Auto-generated method stub
//					
//				}
//
//				@Override
//				public void onOffline() {
//					// TODO Auto-generated method stub
//					
//				}};
//		}
//    };
//     
//    private SendMessageListener(Parcel in) {
//       requestCode = RequestCode.values()[in.readInt()];
//    }

}
